import { createRouter, createWebHistory } from "vue-router";
import Home from "../views/Home.vue";

const routes = [{
    path: '/',
    redirect: '/index'
}, {
    path: "/",
    name: "Home",
    component: Home,
    children: [{
            path: "/index",
            name: "index",
            meta: {
                title: '系统首页'
            },
            component: () =>
                import (
                    "../views/Index.vue")
        }, {
            path: "/user",
            name: "user",
            meta: {
                title: '用户管理'
            },
            component: () =>
                import (
                    "../views/User.vue")
        }, {
            path: "/goods",
            name: "goods",
            meta: {
                title: '商品管理'
            },
            component: () =>
                import (
                    "../views/Goods.vue")
        }, {
            path: "/order",
            name: "order",
            meta: {
                title: '订单管理'
            },
            component: () =>
                import (
                    "../views/BaseForm.vue")
        }, {
            path: "/icon",
            name: "icon",
            meta: {
                title: '自定义图标'
            },
            component: () =>
                import (
                    "../views/Icon.vue")
        }, {
            path: '/404',
            name: '404',
            meta: {
                title: '找不到页面'
            },
            component: () =>
                import (
                    '../views/404.vue')
        }, {
            path: '/403',
            name: '403',
            meta: {
                title: '没有权限'
            },
            component: () =>
                import (
                    '../views/403.vue')
        },
        {
            path: '/permission',
            name: 'permission',
            meta: {
                title: '权限测试'
            },
            component: () =>
                import (
                    '../views/Permission.vue')
        },
        {
            path: '/goodsSku',
            name: 'goodsSku',
            meta: {
                title: '商品属性'
            },
            component: () =>
                import (
                    '../views/GoodsSku.vue')
        },
        {
            path: '/category',
            name: 'category',
            meta: {
                title: '商品类别'
            },
            component: () =>
                import (
                    '../views/Category.vue')
        }

    ]
}, {
    path: "/login",
    name: "Login",
    meta: {
        title: '登录'
    },
    component: () =>
        import (
            "../views/Login.vue")
}];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 茶坊小舍`;
    const role = localStorage.getItem('ms_username');
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        console.log(role);
        role === 'admin' ?
            next() :
            next('/403');
    } else {
        next();
    }
});

export default router;